<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="智红的博客">
    <meta name="keyword"  content="智红">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        python_pandas - 智红的博客
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/css/aircloud.css">

    
<link rel="stylesheet" href="/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 5.3.0"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 好记性不如烂键盘呐 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Zhihong Li</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 好记性不如烂键盘呐 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        python_pandas
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-08-08 14:45:30</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#python" title="python">python</a>
        <span>/</span>
        
        <a class="tag" href="/tags/#pandas" title="pandas">pandas</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <p>修改集合内匹配A列的行中其他列的值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">&#x27;class.csv&#x27;</span>, header=<span class="number">0</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">  name  age</span><br><span class="line"><span class="number">0</span>   小明   <span class="number">12</span></span><br><span class="line"><span class="number">1</span>   小红   <span class="number">11</span></span><br><span class="line"><span class="number">2</span>   小花   <span class="number">12</span></span><br><span class="line"><span class="number">3</span>   小龙   <span class="number">13</span></span><br><span class="line"><span class="number">4</span>   小白   <span class="number">11</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>femalels=&#123;<span class="string">&quot;小红&quot;</span>,<span class="string">&quot;小花&quot;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>femalels</span><br><span class="line">&#123;<span class="string">&#x27;小花&#x27;</span>, <span class="string">&#x27;小红&#x27;</span>&#125;</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[<span class="string">&#x27;gender&#x27;</span>] = <span class="string">&#x27;male&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">  name  age gender</span><br><span class="line"><span class="number">0</span>   小明   <span class="number">12</span>   male</span><br><span class="line"><span class="number">1</span>   小红   <span class="number">11</span>   male</span><br><span class="line"><span class="number">2</span>   小花   <span class="number">12</span>   male</span><br><span class="line"><span class="number">3</span>   小龙   <span class="number">13</span>   male</span><br><span class="line"><span class="number">4</span>   小白   <span class="number">11</span>   male</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.loc[df.name.isin(femalels), <span class="string">&#x27;gender&#x27;</span>] = <span class="string">&#x27;female&#x27;</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">  name  age  gender</span><br><span class="line"><span class="number">0</span>   小明   <span class="number">12</span>    male</span><br><span class="line"><span class="number">1</span>   小红   <span class="number">11</span>  female</span><br><span class="line"><span class="number">2</span>   小花   <span class="number">12</span>  female</span><br><span class="line"><span class="number">3</span>   小龙   <span class="number">13</span>    male</span><br><span class="line"><span class="number">4</span>   小白   <span class="number">11</span>    male</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.to_csv(<span class="string">&#x27;class_tmp.csv&#x27;</span>, index=<span class="number">0</span>)	<span class="comment">#df.to_excel</span></span><br></pre></td></tr></table></figure>

<p>挑选A列匹配以’xiao‘开头或者中间’带”-“的所有行，把A按字符”-“拆分为两列</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df = pd.read_csv(<span class="string">&#x27;class.csv&#x27;</span>, header=<span class="number">0</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df</span><br><span class="line">  name  age   nickname</span><br><span class="line"><span class="number">0</span>   小明   <span class="number">12</span>  xiao-ming</span><br><span class="line"><span class="number">1</span>   小红   <span class="number">11</span>  xiao-hong</span><br><span class="line"><span class="number">2</span>   小花   <span class="number">12</span>   xiao-hua</span><br><span class="line"><span class="number">3</span>   小龙   <span class="number">13</span>  xiao-long</span><br><span class="line"><span class="number">4</span>   小白   <span class="number">11</span>   xiao-bai</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[df.nickname.<span class="built_in">str</span>.startswith(<span class="string">&#x27;xiao&#x27;</span>)]</span><br><span class="line">  name  age   nickname</span><br><span class="line"><span class="number">0</span>   小明   <span class="number">12</span>  xiao-ming</span><br><span class="line"><span class="number">1</span>   小红   <span class="number">11</span>  xiao-hong</span><br><span class="line"><span class="number">2</span>   小花   <span class="number">12</span>   xiao-hua</span><br><span class="line"><span class="number">3</span>   小龙   <span class="number">13</span>  xiao-long</span><br><span class="line"><span class="number">4</span>   小白   <span class="number">11</span>   xiao-bai</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df[(df.nickname.<span class="built_in">str</span>.contains(<span class="string">&#x27;-&#x27;</span>))]</span><br><span class="line">  name  age   nickname</span><br><span class="line"><span class="number">0</span>   小明   <span class="number">12</span>  xiao-ming</span><br><span class="line"><span class="number">1</span>   小红   <span class="number">11</span>  xiao-hong</span><br><span class="line"><span class="number">2</span>   小花   <span class="number">12</span>   xiao-hua</span><br><span class="line"><span class="number">3</span>   小龙   <span class="number">13</span>  xiao-long</span><br><span class="line"><span class="number">4</span>   小白   <span class="number">11</span>   xiao-bai</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>df.nickname.<span class="built_in">str</span>.split(<span class="string">&#x27;-&#x27;</span>, expand=<span class="literal">True</span>)</span><br><span class="line">      <span class="number">0</span>     <span class="number">1</span></span><br><span class="line"><span class="number">0</span>  xiao  ming</span><br><span class="line"><span class="number">1</span>  xiao  hong</span><br><span class="line"><span class="number">2</span>  xiao   hua</span><br><span class="line"><span class="number">3</span>  xiao  long</span><br><span class="line"><span class="number">4</span>  xiao   bai</span><br><span class="line">&gt;&gt;&gt;</span><br></pre></td></tr></table></figure>
<p>df.bs=df.bs.fillna(‘unknown’)</p>
<p>df.bs=df.bs.replace(‘CHN-UNICO’,’CHN-UNICOM’)</p>
<p>sshpass -p evpower602! ssh -o stricthostkeychecking=no root@${line} ‘reboot -f’</p>

        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        

        

    </ul>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
